package com.hotai.comment.fastDFS;


import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.net.URL;
import java.util.Date;

@Data
@Configuration
public class AliyunConfig {
    @Value("${aliyun.accessKeyId}")
    private String accessKeyId;
    @Value("${aliyun.accessKeySecret}")
    private String accessKeySecret;

    @Value("${aliyun.oss.bucketName}")
    private String bucketName;
    @Value("${aliyun.oss.urlPrefix}")
    private String urlPrefix;
    // 地域节点
    @Value("${aliyun.oss.endpoint}")
    private String endpoint;
    @Bean
    public OSSClient ossClient() {
        return new OSSClient(endpoint, accessKeyId, accessKeySecret);
    }


    public String getUrl(String bucketName, String path) {
        // 设置URL过期时间为10年  3600L*1000*24*365*10
        Date expiration = new Date(System.currentTimeMillis() + 3600L * 1000 * 24 * 365 * 10);
        // 生成URL
        URL url = ossClient().generatePresignedUrl(bucketName, path, expiration);
        return url.toString().substring(0, url.toString().lastIndexOf("?"));

    }
}
